package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.SmsHomeRecommendProduct;
import cn.zcscloud.mall.mbg.model.SmsHomeRecommendSubject;
import cn.zcscloud.mall.service.SmsHomeRecommendProductService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 首页人气推荐管理Controller
 */
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/home/recommendProduct")
public class SmsHomeRecommendProductController {
    @Autowired
    private SmsHomeRecommendProductService smsHomeRecommendProductService;

    /**
     * 添加首页推荐
     * @param homeRecommendProductList
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody List<SmsHomeRecommendProduct> homeRecommendProductList){
        int count = smsHomeRecommendProductService.saveList(homeRecommendProductList);
        if (count > 0) {
            return R.success("");
        }
        return R.error("所选商品均已添加，不可重复操作");
    }

    @PostMapping("/delete")
    public R delete(@RequestParam List<Long> ids){
        boolean b = smsHomeRecommendProductService.removeByIds(ids);
        if (b) return R.success("操作成功");
        return R.error();
    }

    /**
     * 分页展示
     * @param productName
     * @param recommendStatus
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/list")
    public R list(String productName,
                  Integer recommendStatus,
                  @RequestParam(defaultValue = "5") Integer pageSize,
                  @RequestParam(defaultValue = "1") Integer pageNum) {
        LambdaQueryWrapper<SmsHomeRecommendProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(productName != null,SmsHomeRecommendProduct::getProductName, productName)
                .eq(recommendStatus != null,SmsHomeRecommendProduct::getRecommendStatus, recommendStatus);
        Page<SmsHomeRecommendProduct> page = new Page<>(pageNum, pageSize);
        Page<SmsHomeRecommendProduct> iPage = smsHomeRecommendProductService.page(page, queryWrapper);
        return R.success(PageRequest.init(iPage));
    }

    /**
     * 批量修改推荐状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public R updateRecommendStatus(@RequestParam("ids") List<Long> ids, @RequestParam Integer recommendStatus) {
        int count = 0;
        SmsHomeRecommendProduct smsHomeRecommendProduct = new SmsHomeRecommendProduct();
        smsHomeRecommendProduct.setRecommendStatus(recommendStatus);
        for (Long id : ids) {
            smsHomeRecommendProduct.setId(id);
            smsHomeRecommendProductService.updateById(smsHomeRecommendProduct);
            count++;
        }
        if (count > 0) {
            return R.success("操作成功");
        }
        return R.error();
    }

    /**
     * 修改推荐排序
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("/update/sort/{id}")
    public R updateRecommendSort(@PathVariable Long id,Integer sort){
        UpdateWrapper<SmsHomeRecommendProduct> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SmsHomeRecommendProduct::getRecommendStatus, id);
        SmsHomeRecommendProduct recommendProduct = new SmsHomeRecommendProduct();
        recommendProduct.setSort(sort);
        recommendProduct.setId(id);
        boolean update = smsHomeRecommendProductService.updateById(recommendProduct);
        if(!update){
            return R.error();
        }
        return R.success("操作成功");
    }


}
